package cn.kgc.controller.logic.administrative;

import cn.kgc.pojo.*;
import cn.kgc.service.administrative.smc_MingPianService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: smc
 * @date: 2022/2/17
 * @description:
 */
@Controller
@RequestMapping("/smc")
public class smc_mingpianController {
    @Resource
    private smc_MingPianService mpService;

    @RequestMapping("/yongzhang")
    public String yongzhang(Model model){
        List<Dict> yongzhang = mpService.selDictAll(5);
        model.addAttribute("yongzhang",yongzhang);
        return "administrative/smc-yz";
    }



    @RequestMapping("/add")
    @ResponseBody
    public boolean add(HttpSession session, @RequestParam String yzTime,
                       @RequestParam Integer d_id,
                       @RequestParam String reason
                       ){
        System.out.println("yzTime ================================== " + yzTime);
        User user = (User)session.getAttribute("user");
        Jobtitle jobtitle = (Jobtitle)session.getAttribute("zhiWei");

        Chapters chapters = new Chapters(-1,user.getId(),jobtitle.getId(),d_id,Date.valueOf(yzTime),reason,"",0);
        boolean b = mpService.addYongZhang(chapters);

        return b;
    }

    @RequestMapping("/addmp")
    @ResponseBody
    public boolean addmp(HttpSession session, @RequestParam String createTime,
                         @RequestParam String phone,
                         @RequestParam Integer number,
                         @RequestParam String reark,
                         @RequestParam String approver) {
        User user = (User)session.getAttribute("user");
        Jobtitle jobtitle = (Jobtitle)session.getAttribute("zhiWei");
        Businesscard businesscard = new Businesscard(-1,user.getId(),jobtitle.getId(),Date.valueOf(createTime),phone,number,reark,approver,0);
        boolean c = mpService.addMingPian(businesscard);
        return c;
    }

    @RequestMapping("/ejld")
    @ResponseBody
    public String ejld(@RequestParam Integer yzName){
        int num = yzName;
        Dict a = mpService.selss(num);
        System.out.println("a============"+a);
        if(num==18){
            List<String> selmz = mpService.selmz(18);
            System.out.println("selmz=============="+selmz);
            return JSON.toJSONString(selmz);
        }

        if(num==19){
            List<String> selmz = mpService.selmz(19);
            return JSON.toJSONString(selmz);
        }
        if(num==20){
            List<String> selmz = mpService.selmz(20);
            return JSON.toJSONString(selmz);
        }else {
            return JSON.toJSONString(a);
        }

    }


    @RequestMapping("/page")
    @ResponseBody
    public String page(HttpSession session, @RequestParam("currentPage") Integer currentPage,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("state") Integer state) {
        PageHelper.startPage(currentPage, pageSize);
        User user = (User)session.getAttribute("user");
        Jobtitle jobtitle = (Jobtitle)session.getAttribute("zhiWei");
       Page<Chapters> chapters = mpService.selChapters(user.getId(),state);

        for (Chapters aa : chapters
        ) {
            System.out.println(aa.toString());
        }

        PageInfo<Chapters> pageInfo = new PageInfo<Chapters>(chapters);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("user",user);
        map.put("tr",pageInfo);
        map.put("jobtitle",jobtitle);


        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }
    @RequestMapping("/pages")
    @ResponseBody
    public String pages(HttpSession session, @RequestParam("currentPage") Integer currentPage,
                       @RequestParam("pageSize") Integer pageSize,
                        @RequestParam("state") Integer state) {
        PageHelper.startPage(currentPage, pageSize);
        User user = (User)session.getAttribute("user");
        Jobtitle jobtitle = (Jobtitle)session.getAttribute("zhiWei");
        Page<Businesscard> businesscards = mpService.selBusinesscard(user.getId(),state);

        for (Businesscard bb : businesscards
        ) {
            System.out.println(bb.toString());
        }

        PageInfo<Businesscard> pageInfo = new PageInfo<Businesscard>(businesscards);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("user",user);
        map.put("tr",pageInfo);
        map.put("jobtitle",jobtitle);
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }

    @RequestMapping("/zbxx")
    public String zbxx(){

        return "administrative/zbxx";
    }

    @RequestMapping("/updYz")
    @ResponseBody
    public boolean updLe(Integer id){
        boolean b = mpService.updYz(id);
        return b;
    }
    @RequestMapping("/updMp")
    @ResponseBody
    public boolean updTr(Integer id){
        boolean b = mpService.updMp(id);
        return b;
    }
}
